//
//  DetailView.m
//  UI12豆瓣大项目
//
//  Created by dllo on 15/10/27.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "DetailView.h"
#import "UIImageView+WebCache.h"
@implementation DetailView
- (void)dealloc
{
    [_timeLabel release];
    [_detailImageView release];
    [_timeImageView release];
    [_owner_name_ImageView release];
    [_category_name_ImageView release];
    [_contentLabel release];
    [_owner_name_Label release];
    [_category_name_Label release];
    [_act_intro_Label release];
    [super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame Activity:(Activity *)activity {
    if ([super initWithFrame:frame])
    {
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 17, 325, 30)];
        self.titleLabel.text = activity.title;
        [self addSubview:self.titleLabel];
        [_timeLabel release];
        
        self.detailImageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 60, 110, 150)];
        //重新请求图片
        NSURL *url = [NSURL URLWithString:activity.image];
        [self.detailImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHoderImage"]];
        [self addSubview:self.detailImageView];
        [_detailImageView release];
        
        self.timeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 60, 20, 20)];
        self.timeImageView.image = [UIImage imageNamed:@"icon_date_blue@2x.png"];
        [self addSubview:self.timeImageView];
        [self.timeImageView release];
        
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(165, 60, 200, 18)];
        self.timeLabel.font = [UIFont systemFontOfSize:15];
        NSString *time = [NSString stringWithFormat:@"时间:%@ %@",activity.begin_time, activity.end_time];
        self.timeLabel.text = time;
        [self addSubview:self.timeLabel];
        [_timeLabel release];
        
        self.owner_name_ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 85, 20, 20)];
        self.owner_name_ImageView.image = [UIImage imageNamed:@"icon_sponsor_blue@2x.png"];
        [self addSubview:self.owner_name_ImageView];
        [self.owner_name_ImageView release];
        
        self.owner_name_Label = [[UILabel alloc] initWithFrame:CGRectMake(165, 85, 200, 18)];
        self.owner_name_Label.font = [UIFont systemFontOfSize:15];
        
        self.owner_name_Label.text = [activity.ownerDic objectForKey:@"name"];
        [self addSubview:self.owner_name_Label];
        [_owner_name_Label release];
        
        self.category_name_ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 110, 20, 20)];
        self.category_name_ImageView.image = [UIImage imageNamed:@"icon_catalog_blue@2x.png"];
        [self addSubview:self.category_name_ImageView];
        [self.category_name_ImageView release];
        
        self.category_name_Label = [[UILabel alloc] initWithFrame:CGRectMake(165, 110, 200, 18)];
        self.category_name_Label.font = [UIFont systemFontOfSize:15];
        NSString *stype = [NSString stringWithFormat:@"类型:%@",activity.category_name];
        self.category_name_Label.text = stype;
        [self addSubview:self.category_name_Label];
        [_category_name_Label release];
        
        self.addressImageView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 135, 20, 20)];
        self.addressImageView.image = [UIImage imageNamed:@"icon_spot_blue@2x.png"];
        [self addSubview:self.addressImageView];
        [self.addressImageView release];
        
        self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(165, 127, 200, 36)];
        NSString *address = [NSString stringWithFormat:@"地点:%@",activity.address];
        self.addressLabel.text = address;
        self.addressLabel.numberOfLines = 0;
        self.addressLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.addressLabel];
        [_addressLabel release];
        
        self.act_intro_Label = [[UILabel alloc] initWithFrame:CGRectMake(25, 220, 325, 40)];
        self.act_intro_Label.text = @"活动介绍";
        self.act_intro_Label.font = [UIFont systemFontOfSize:20];
        self.act_intro_Label.textColor = [UIColor grayColor];
        [self addSubview:self.act_intro_Label];
        [_act_intro_Label release];
        
        self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 280, 325, 200)];
        [self.contentLabel setFont:[UIFont systemFontOfSize:15]];
        self.contentLabel.text = activity.content;
        self.contentLabel.numberOfLines = 0;
        
#pragma mark --??
        //自适应Label***要先有内容  但是下边有大空格
        CGFloat height = [DetailView heightForContent:self.contentLabel.text];
        CGRect tempFrame = self.contentLabel.frame;
        tempFrame.size.height = height;  //结构体中给成员变量赋值
        self.contentLabel.frame = tempFrame;
        
        
        
        [self.contentLabel sizeToFit];
        [self addSubview:self.contentLabel];
        [_contentLabel release];
        
//        写在view里面不太好用UIAlertController
        
//        self.starbutton = [UIButton buttonWithType:UIButtonTypeCustom];
//        self.starbutton.frame = CGRectMake(165, 180, 20, 20);
//        //    self.starbutton.backgroundColor = [UIColor blackColor];
//        [self.starbutton setBackgroundImage:[UIImage imageNamed:@"ic_star_half@2x"] forState:UIControlStateNormal];         //别用错方法了!
//        [self.starbutton setBackgroundImage:[UIImage imageNamed:@"ic_star_red@2x"] forState:UIControlStateSelected];
//        [self.starbutton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:self.starbutton];
        
        

    }
    return self;
}

//算内容高度
+(CGFloat)heightForContent:(NSString *)content{  //声明一个加号方法(不用alloc,比较方便)
    
    //参数1.设置计算高度按照什么样的size来计算(告诉系统按照什么宽度区计算高度)
    //加号方法里面不能用self
    //宽度一定与label宽度一致,高度尽量大一些
    //参数2.设置计算类型
    //参数3.设置计算内容按照多大的字体计算
    //注意:字体大小也要跟label字体一样
    //参数4.系统备用参数,设置为nil就可以了
#warning 字号 和宽度要一直
    CGSize size = CGSizeMake(325, 100000000);
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15.0], NSFontAttributeName, nil];
    CGRect frame = [content boundingRectWithSize: size options:NSStringDrawingUsesLineFragmentOrigin  attributes:dic context:nil];
    return frame.size.height;
}


//-(void)buttonAction:(UIButton *)button{
////    self.starbutton.selected = !self.starbutton.selected; //取反
//    if (button.selected == NO) {
////        UIAlertController *allert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
//        [alertV show];
//        button.selected = YES;
//        
//    }else{
//        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
//        [alertV show];
//        button.selected = NO;
//    }
//}
@end
